/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filtermanagement;

import FilterCommon.*;
//import filtermanagement.Filters.FilterView;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.TreePath;

public class FilterPanel extends JPanel implements KeyListener, MouseListener, MouseMotionListener {


    private FilterView [] _FilterViewTab = new FilterView[4];
//
//    private static final int countBall = 4;
//
//    private static final int BALL_DIAMETER = 50; // Diameter of ball
//    //--- instance variables
//    /** Ball coords.  Changed by mouse listeners.  Used by paintComponent. */
//    private int []_ballX   = new int[countBall];   // x coord - set from drag
//    private int []_ballY   = new int[countBall];   // y coord - set from drag
//
//
//    /** Position in ball of mouse press to make dragging look better. */
//    private int []_dragFromX = new int[countBall];    // pressed this far inside ball's
//    private int []_dragFromY = new int[countBall];    // bounding box.
//
//    /** true means mouse was pressed in ball and still in panel.*/
//    private boolean []_selected = new boolean[countBall];

    private boolean _ShiftPressed = false;

    private int _mousePressedX;
    private int _mousePressedY;


    //============================================================= constructor
    /** Constructor sets size, colors, and adds mouse listeners.*/
    public FilterPanel() {
        setPreferredSize(new Dimension(300, 300));
        setBackground(Color.white);
        //setForeground(Color.yellow);
        //--- Add the mouse listeners.
        this.addMouseListener(this);
        this.addMouseMotionListener(this);

        this.addKeyListener(this);
        this.setFocusable(true);

        for(int i=0; i<_FilterViewTab.length; i++)
        {
            _FilterViewTab[i] = new FilterView(2,2);
        }
//        for(int i=0; i<countBall; i++)
//        {
//
//            _ballX[i]=300/countBall*i;
//            _ballY[i]=300/countBall*i;
//
//            _dragFromX[i]=0;
//            _dragFromY[i]=0;
//
//            _selected[i] = false;
//
//        }
    }//endconstructor

    public void addFilter(String s)
    {   System.out.print(s);
    }
    //=================================================== method paintComponent
    /** Ball is drawn at the last recorded mouse listener coordinates. */
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);   // Required for background.

        for(int i=_FilterViewTab.length-1; i>=0; i--)
        {
            _FilterViewTab[i].paintFilter(g);
        }


    }//end paintComponent

    //===================================================== method mousePressed
    /** Set _canDrag if the click is in the ball (or in the bounding
        box, which is lazy, but close enuf for this program).
        Remember displacement (dragFromX and Y) in the ball
        to use as relative point to display while dragging.
    */
    public void mousePressed(MouseEvent e) {
//        _mousePressedX = e.getX();   // Save the x coord of the click
//        _mousePressedY = e.getY();   // Save the y coord of the click
//        boolean ifDrag = false;
//
//        for(int i=0; i<countBall; i++)
//        {
//            if(onBall(i,_mousePressedX, _mousePressedY) && _selected[i])
//            {
//                ifDrag = true;
//            }
//        }
//        if(!ifDrag && !_ShiftPressed)
//        {
//            for(int i=0; i<countBall; i++)
//            {
//                _selected[i] = false;
//            }
//            for(int i=0; i<countBall; i++)
//            {
//                if (onBall(i,_mousePressedX, _mousePressedY))
//                {
//                    _selected[i] = true;
//                    ifDrag = true;
//                    break;
//                }
//            }
//        }
//
//        if(ifDrag)
//        {
//            for(int i=0; i<countBall; i++)
//            {
//                if(_selected[i])
//                {
//                    _dragFromX[i] = _mousePressedX - _ballX[i];  // how far from left
//                    _dragFromY[i] = _mousePressedY - _ballY[i];  // how far from top
//                }
//            }
//        }
//
//        this.repaint();
    }//end mousePressed

    //============================================================ mouseDragged
    /** Set x,y  to mouse position and repaint. */
    public void mouseDragged(MouseEvent e)
    {
//        if(!_ShiftPressed)
//        {
//            for(int i=0; i<countBall; i++)
//            {
//                if (_selected[i]) {   // True only if button was pressed inside ball.
//                    //--- Ball pos from mouse and original click displacement
//                    _ballX[i] = e.getX() - _dragFromX[i];
//                    _ballY[i] = e.getY() - _dragFromY[i];
//
//                    //--- Don't move the ball off the screen sides
//                    _ballX[i] = Math.max(_ballX[i], 0);
//                    _ballX[i] = Math.min(_ballX[i], getWidth() - BALL_DIAMETER);
//
//                    //--- Don't move the ball off top or bottom
//                    _ballY[i] = Math.max(_ballY[i], 0);
//                    _ballY[i] = Math.min(_ballY[i], getHeight() - BALL_DIAMETER);
//
//                }
//            }
//            this.repaint();
//        }
    }//end mouseDragged

    //====================================================== method mouseExited
    /** Turn off dragging if mouse exits panel. */
    public void mouseExited(MouseEvent e) {
//        for(int i=0; i<countBall; i++)
//        {
//            _canDrag[i] = false;
//        }
    }//end mouseExited

    //=============================================== Ignore other mouse events.
    public void mouseMoved   (MouseEvent e) {}  // ignore these events
    public void mouseEntered (MouseEvent e) {}  // ignore these events
    public void mouseClicked (MouseEvent e)
    {
//        //jeśtli nie wciśnięto shitf to odznaczamy wszystko
//        if(!_ShiftPressed)
//        {
//            for(int i=0; i<countBall; i++)
//            {
//                _selected[i] = false;
//            }
//        }
//        //zaznaczamy to na czym kliknęliśmy
//        for(int i=0; i<countBall; i++)
//        {
//            if (onBall(i,_mousePressedX, _mousePressedY))
//            {
//                if(_ShiftPressed)
//                {   _selected[i] = !_selected[i];
//                }
//                else
//                {   _selected[i] = true;
//                }
//               // ifMark = true;
//                break;
//            }
//        }
//        repaint();
    }  // ignore these events
    public void mouseReleased(MouseEvent e) {
//        for(int i=0; i<countBall; i++)
//        {
//            _canDrag[i] = false;
//        }
    }  // ignore these events

    public void keyTyped(KeyEvent k) {}

    public void keyPressed(KeyEvent k)
    {
        if(k.getKeyCode()==KeyEvent.VK_SHIFT)
        {   _ShiftPressed = true;
        }
    }
    public void keyReleased(KeyEvent k)
    {
        if(k.getKeyCode()==KeyEvent.VK_SHIFT)
        {   _ShiftPressed = false;
        }
    }

}